package service

import (
	"seat-service/initialization"
	"seat-service/model"
)

type InformationService struct {
}

func (*InformationService) QueryInformation(id uint) (model.Student, error) {
	var student model.Student
	err := initialization.DB.Model(&model.Student{}).Where("id=?", id).Scan(&student).Error
	if err != nil {
		return model.Student{}, err
	}
	return student, nil
}

func (*InformationService) RecordInformation(id, page, pageSize uint) ([]model.Reserve, error) {
	s := make([]model.Reserve, 0)
	err := initialization.DB.Model(&model.Reserve{}).Where("student_id=?", id).Find(&s).Error
	if err != nil {
		return nil, err
	}
	return s, nil
}

func (*InformationService) FeedbackInformation(feedback model.Feedback) error {
	err := initialization.DB.Create(&feedback).Error
	if err != nil {
		return err
	}
	return nil
}

func (*InformationService) UpHead(file string, id uint) error {
	err := initialization.DB.Model(&model.Student{}).Where("id=?", id).Update("head", file).Error
	if err != nil {
		return err
	}
	return nil
}

func (s *InformationService) CreditRecordInformation(id uint) (int64, int64, error) {
	//TODO 接口一  查询当前学生的预约记录表，根据预约记录的状态判断用户是否违约  预约记录表 0 已预约  1 签到成功  2/3 不知道啥意思
	// 签到时进行条件判断，如果超时就修改预约记录状态，记一次违规次数  ， 还有就是后端定时检查数据库预约记录状态为0的记录表，检查预约结束时间，发现一直未签到，超过签到时间的就修改预约记录表状态
	// 返回违规次数和总预约次数
	var count1 int64 = 0
	err := initialization.DB.Model(&model.Reserve{}).Where("student_id=?", id).Count(&count1).Error
	if err != nil {
		return -1, -1, err
	}
	var count2 int64 = 0
	err = initialization.DB.Model(&model.Reserve{}).Where("student_id=? and reserve_status = ? ", id, 2).Count(&count2).Error
	if err != nil {
		return -1, -1, err
	}
	return count1, count2, err

}
